package cn.wolfcode.web.controller;


import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OperateIntergralVo;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.domain.PayLog;
import cn.wolfcode.domain.PayVo;
import cn.wolfcode.feign.AliPayApi;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.IOrderInfoService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    //orderPay/alipay?orderNo=1467059354325745664&type=0
    @RequestMapping("/alipay")
    public Result<String> alipay(HttpServletRequest request,String orderNo, Integer type){
        String token = request.getHeader("token");
        OrderInfo orderInfo = orderInfoService.find(token, orderNo);
        //1、判断是积分支付还是在线支付
        if(OrderInfo.PAYTYPE_ONLINE==type){
            Result<String> result=orderInfoService.onlinePay(orderInfo);
            return Result.success(result.getData());//返回支付表单（包含各种参数）
        }else {
            //积分支付
            orderInfoService.integralPay(orderInfo);
            return Result.success("");//页面刷新
        }
    }
    //提供支付宝同步回调接口：http://gsagi3.natappfree.cc/seckill/orderPay/return_url
    //一般用来重定向显示订单详情.
    @RequestMapping("/return_url")
    public void return_url(HttpServletResponse response, HttpServletRequest request, @RequestParam Map<String,String> params) throws IOException {
        System.out.println(request.getParameterMap());
        System.out.println(params);

        String out_trade_no = params.get("out_trade_no");//订单号
        //1、验签操作
        Result<Boolean> result=orderInfoService.rshcheck(params);
        Boolean signVerified=result.getData();
        if(signVerified){
            //验签成功，重定向到订单详情
            response.sendRedirect(frontEndPayUrl+out_trade_no);//http://localhost/order_detail.html?orderNo=
        }else {
            response.getWriter().println("验签失败！");
        }
    }
    //异步回调： http://gsagi3.natappfree.cc/seckill/orderPay/notify_url
    //一般订单支付成功之后，需要完成的业务逻辑.
    @RequestMapping("/notify_url")
    @ResponseBody
    public String notify_url(HttpServletRequest request,@RequestParam Map<String,String> params){//特别注意异步回调参数为text格式的.
        System.out.println(request.getParameterMap());
        String success=orderInfoService.notify_url(params);
        return success;
    }

    //orderPay/refund?orderNo=1467117332697448453
    //申请退款:
    @RequestMapping("/refund")
    public Boolean refundOrder(String orderNo){
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        //判断订单状态 退款类型
        if(OrderInfo.PAYTYPE_ONLINE==orderInfo.getPayType()){
            //在线金额退款
            Result<Boolean> isRefund= orderInfoService.refundOrder(orderNo);
            return isRefund.getData();
        }else {
            //在线积分退款
            Result<Boolean> isIntergralRefund=orderInfoService.intergralRefundOrder(orderInfo);
            return isIntergralRefund.getData();
        }
    }

}
